﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDemo
{
    public partial class Form1 : Form
    {
        private int xPos;
        private int yPos;

        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
        }

        public void RunJs(String js)
        {
            if (webView21.CoreWebView2 != null)
                webView21.CoreWebView2.ExecuteScriptAsync(js);
        }

        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            MessageBox.Show("拖拽了");
        }

        private void pictureBox1_Move(object sender, EventArgs e)
        {
        }

        private void webView21_DragEnter(object sender, DragEventArgs e)
        {
            MessageBox.Show("来了1");
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            //MessageBox.Show("来了");
        }

        private void pictureBox1_DragLeave(object sender, EventArgs e)
        {
            MessageBox.Show("来了");
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // 鼠标按下拖拽图片
                if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
                {
                    // 限制拖拽出框
                    //if ((pictureBox1.Width - this.Width) >= 0 || (pictureBox1.Height - this.Height) >= 0)
                    //{
                    //if ((pictureBox1.Top + Convert.ToInt16(e.Y - yPos)) >= 0
                    //    || (pictureBox1.Left + Convert.ToInt16(e.X - xPos)) >= 0
                    //    || (pictureBox1.Right + Convert.ToInt16(e.X - xPos)) <= this.Width
                    //    || (pictureBox1.Bottom + Convert.ToInt16(e.Y - yPos)) <= this.Height)
                    //{
                    pictureBox1.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
                    pictureBox1.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
                                                                   //    }
                                                                   //}
                }
            }
            catch (Exception dd)
            {
                MessageBox.Show(dd.Message);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            xPos = e.X;//当前x坐标.
            yPos = e.Y;//当前y坐标.
        }

        private void pictureBox1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
        }

        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListView IstView = sender as ListView;
            if (IstView != null)
            {
                IstView.DoDragDrop(IstView.SelectedItems, DragDropEffects.Move);
            }
        }

        private void listView2_DragLeave(object sender, EventArgs e)
        {
            ListView IstView = sender as ListView;
            if (IstView != null)
            {
                foreach (ListViewItem item in IstView.SelectedItems)
                {
                    IstView.Items.Remove(item);
                }
            }
        }

        private void listView2_DragDrop(object sender, DragEventArgs e)
        {
            ListView IstView = sender as ListView;
            ListView.SelectedListViewItemCollection items = (ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
            if (items != null && items.Count > 0)
            {
                foreach (ListViewItem item in items)
                {
                    ListViewItem itemTep = (ListViewItem)item.Clone();
                    itemTep.Name = item.Text;
                    if (!IstView.Items.ContainsKey(itemTep.Name))
                    {
                        IstView.Items.Add(itemTep);
                    }
                }
            }
        }

        private void listView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //listView1.ItemDrag += new ItemDragEventHandler(listView1_ItemDrag);
            //listView2.ItemDrag += new ItemDragEventHandler(listView1_ItemDrag);

            //listView2.DragLeave += new EventHandler()
            //listView2.DragDrop
            ClockControl clock = new ClockControl();
            panel1.Controls.Add(clock);
            listView1.Items.AddRange(new ListViewItem[]
            {
                    new ListViewItem("one"),
                    new ListViewItem("two"),
                    new ListViewItem("thress"),
                    new ListViewItem("four"),
                    new ListViewItem("five"),
                    new ListViewItem("six")
            });
            init();
        }

        private async void init()
        {
            await webView21.EnsureCoreWebView2Async();
            string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HTMLPage1.html");
            webView21.Source = new Uri("file:///D:/ProgramFiles/DragDemo/DragDemo/HTMLPage1.html");

            webView21.CoreWebView2.NewWindowRequested += (sender2, e2) =>
            {
                String _fileurl = e2.Uri.ToString();
                e2.Handled = true;
                RunJs($"var temp_dropObject = \"{_fileurl}\"");
            };
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            ListBox IstView = sender as ListBox;

            ListView.SelectedListViewItemCollection items = (ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
            if (items != null && items.Count > 0)
            {
                foreach (ListViewItem item in items)
                {
                    if (!IstView.Items.Contains(item.Text))
                    {
                        IstView.Items.Add(item.Text);
                    }
                }
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
        }

        private void webView21_DragDrop(object sender, DragEventArgs e)
        {
            MessageBox.Show("123");
        }

        private void webView21_DragLeave(object sender, EventArgs e)
        {
            MessageBox.Show("123");
        }

        private void webView21_ControlAdded(object sender, ControlEventArgs e)
        {
            MessageBox.Show("123");
        }

        private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // webView21.CoreWebView2.PostWebMessageAsString("123");
            //MyObject my = new MyObject("123", "[[[");
            //string jhson = JsonConvert.SerializeObject(my);
            //webView21.CoreWebView2.PostWebMessageAsString(jhson);

            TestDragForm form = new TestDragForm();
            form.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selectedListView = listView1.SelectedItems;
            if (selectedListView.Count == 1)
            {
                ListViewItem listViewItem = selectedListView[0];
                string jhson = JsonConvert.SerializeObject(listViewItem.Text);
                webView21.CoreWebView2.PostWebMessageAsString(jhson);
            }
        }
    }
}